package org.icdio.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.icdio.dao.User;
import org.icdio.entity.SimpleUser;
import org.icdio.utils.DBUtils;

public class UserImpl implements User{



	Connection conn;

	/**
	 * 根据ID查询指定结果
	 * @param id
	 * @return
	 * 2018年8月5日上午11:09:56
	 * name:官江涛
	 */
	@Override
	public SimpleUser selectById(Integer id) {

		conn = DBUtils.open();
		List<SimpleUser> result= new ArrayList<>();
		String sql = " SELECT * FROM  simple_user where id = ? ";
		try {
			PreparedStatement pre;
			pre = conn.prepareStatement(sql);
			pre.setInt(1, id);
			ResultSet rSet = pre.executeQuery();
			SimpleUser simpleUser = new SimpleUser();
			while(rSet.next()) {
				simpleUser.setId(rSet.getString(1));
				simpleUser.setUserId(rSet.getString(2));
				simpleUser.setUserName(rSet.getString(3));
				simpleUser.setPassword(rSet.getString(4));
				simpleUser.setCreateTime(rSet.getString(5));
			}
			return simpleUser;
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * ��ѯ���м��� 
	 * @return
	 * 2018��8��5������10:29:33
	 * name:�ٽ���
	 */
	@Override
	public List selectAll() {
		// ��������
		conn = DBUtils.open();
		List<SimpleUser> result= new ArrayList<>();
		String sql = " SELECT * FROM  simple_user";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet rset;
			rset=stmt.executeQuery(sql);
			while(rset.next()) {
				SimpleUser simpleUser = new SimpleUser();
				simpleUser.setId(rset.getString(1));
				simpleUser.setUserId(rset.getString(2));
				simpleUser.setUserName(rset.getString(3));
				simpleUser.setPassword(rset.getString(4));
				simpleUser.setCreateTime(rset.getString(5));
				result.add(simpleUser);
			}
			return result;
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;	
	}

}
